﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_Public;
using QLTV_BLL;

namespace QLTV_GiaoDien
{
    public partial class frm_Lop_DonVi_Them : DevExpress.XtraEditors.XtraForm
    {
        public frm_Lop_DonVi_Them()
        {
            InitializeComponent();
        }
        private int _flag;

        public int Flag
        {
            get { return _flag; }
            set { _flag = value; }
        }
        private int _MaDonVi;

        public int MaDonVi
        {
            get { return _MaDonVi; }
            set { _MaDonVi = value; }
        }
        private string _TenDonVi;

        public string TenDonVi
        {
            get { return _TenDonVi; }
            set { _TenDonVi = value; }
        }
        private string _GhiChu;

        public string GhiChu
        {
            get { return _GhiChu; }
            set { _GhiChu = value; }
        }
        public delegate void GetInt(int kt);
        public GetInt GetLoad;
        public void Huy_Text(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                ctr.Text = string.Empty;
            }
            foreach (Control i in ctr.Controls)
                Huy_Text(i);
        }
        public void GetDataSua()
        {
            txt_MaKhoa.Text = MaDonVi.ToString();
            txt_DonVi.Text = TenDonVi;
            txt_GhiChu.Text = GhiChu;
        }
        public void LoadForm()
        {
            if (Flag == 0)
            {
                this.Text = "Thêm Đơn Vị";
            }
            if (Flag == 1)
            {
                this.Text = "Sửa Đơn Vị";
                GetDataSua();
            }
        }
        DonVi_BLL donvi_bll = new DonVi_BLL();
        private void frm_Lop_DonVi_Them_Load(object sender, EventArgs e)
        {
            LoadForm();
        }
        NhatKy_BLL nhatKy_bll = new NhatKy_BLL();
        NhatKy_Public nk = new NhatKy_Public();
        TrungGian tg = new TrungGian();
        private void btn_Luu_Click(object sender, EventArgs e)
        {
            DonVi_Public dv = new DonVi_Public();
            if (Flag == 0)
            {
                //Them
                dv.TenDonVi=txt_DonVi.Text;
                dv.GhiChu=txt_GhiChu.Text;
                donvi_bll.Insert_DonVi(dv,dv);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Thêm";
                nk.MoTa = "Thêm thông tin Đơn vị: '" + txt_DonVi.Text + "'";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Thêm mới thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            if (Flag == 1)
            {
                //Sua
                dv.MaDonVi=Convert.ToInt32(txt_MaKhoa.Text);
                dv.TenDonVi=txt_DonVi.Text;
                dv.GhiChu=txt_GhiChu.Text;
                donvi_bll.Update_DonVi(dv,dv,dv);
                //
                string d = DateTime.Now.ToString();
                nk.MaNV = tg.MaNV;
                nk.NgayGio = d;
                nk.HanhDong = "Sửa";
                nk.MoTa = "Sửa thông tin Đơn vị: '" +TenDonVi.ToString() + "' thành: '"+txt_DonVi.Text+"' ";
                nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                //
                XtraMessageBox.Show("Sửa thành công!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            GetLoad(1);
            Huy_Text(this);
        }

        private void btn_Huy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_MaKhoa_Leave(object sender, EventArgs e)
        {
            if (txt_MaKhoa.Text == "")
                txt_MaKhoa.Focus();
        }

        private void txt_MaKhoa_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }
    }
}